package com.woniu.car.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.bean.R;
import com.woniu.car.bean.TCar;
import com.woniu.car.bean.TCarType;
import com.woniu.car.bean.TCarrier;
import com.woniu.car.service.ITCarService;
import com.woniu.car.service.ITCarTypeService;
import com.woniu.car.service.ITCarrierService;
import com.woniu.car.util.CarNum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kk
 * @since 2022-08-09
 */
@RestController
@RequestMapping("/car")
@CrossOrigin
public class TCarController {
    @Autowired
    private ITCarService carService;
    @Autowired
    private ITCarrierService carrierService;
    @Autowired
    private ITCarTypeService carTypeService;

//    @GetMapping
//    public R selectAll(Integer currentPage, Integer pageSize){
//        R r =null;
//
//
//
//        IPage page=new Page(currentPage,pageSize);
//        IPage page1 = carService.page(page);
//        long total = page1.getTotal();
//        List<Car> records = page1.getRecords();
//
//        records.forEach(e->{
//
//            Carrier carrier = carrierService.getById(e.getCarrierId());
//            e.setCarrier(carrier);
//            CarType carType = carTypeService.getById(e.getCartypeId());
//            e.setCarTypes(carType);
//
//        });
//
//
//
//        Map map =new HashMap<>();
//        map.put("total",total);
//        map.put("records",records);
//        r=R.ok();
//        r.setData(map);
//        return r;
//    }
    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable Integer id){
        TCar car = carService.getById(id);
        boolean b = carService.removeById(id);
        R r =null;
        if(b){
            r=R.OK();
            //修改总数
            Integer carrierId = car.getCarrierId();
            TCarrier TCarrier = carrierService.getById(carrierId);
            TCarrier.setCarTotal(TCarrier.getCarTotal()-1);
            carrierService.updateById(TCarrier);
        }else {
            r=R.ERROR();
        }
        return r;
    }
    //新增车辆
    @PostMapping
    public R addCar(@RequestBody TCar car){
        //随机车辆编号
        CarNum carNum =new CarNum();
        Integer random = carNum.getRandom();
        car.setCarNum(random);

        boolean save = carService.save(car);
        R r =null;
        if(save){
            r=R.OK();
            //修改货运商车辆总数
            Integer carrierId = car.getCarrierId();
            TCarrier TCarrier = carrierService.getById(carrierId);
            TCarrier.setCarTotal(TCarrier.getCarTotal()+1);
            carrierService.updateById(TCarrier);
        }else {
            r=R.ERROR();
        }
        return r;
    }
    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id){
        TCar car = carService.getById(id);
        R r =null;
        r=R.OK();
        r.setData(car);
        return r;

    }
    //修改车辆数据，加乐观锁
    @PutMapping
    public R updateCar(@RequestBody TCar car){
        R r =null;
        //修改对象的版本号
        Integer version = car.getVersion();
        Integer id = car.getCarId();
        //数据库中原对象
        TCar car1 = carService.getById(id);
        //数据库中的版本号
        Integer version1 = car1.getVersion();
        if(version1 != version){
            r =R.ERROR();
            return r;
        }else {
            car.setVersion(car.getVersion()+1);
            boolean b = carService.updateById(car);

            if(b){
                r=R.OK();
            }else {
                r=R.ERROR();
            }
            return r;
        }


    }

    @GetMapping
    public R getByPlate(@RequestParam Integer currentPage, Integer pageSize,String carPlate,Integer cartypeId){
        IPage<TCar> page = new Page<>(currentPage,pageSize);
        QueryWrapper<TCar> qw = new QueryWrapper<>();
        if(StringUtils.hasText(carPlate)){
            qw.like("car_plate",carPlate);
        }
        if(cartypeId != null){
            qw.like("cartype_id",cartypeId);
        }
        IPage<TCar> page1 = carService.page(page, qw);
        List<TCar> records = page1.getRecords();
        records.forEach(e->{
            TCarType carType = carTypeService.getById(e.getCartypeId());
            e.setCarTypes(carType);
            TCarrier TCarrier = carrierService.getById(e.getCarrierId());
            e.setCarrier(TCarrier);
        });
        long total = page1.getTotal();
        Map map = new HashMap();
        map.put("total",total);
        map.put("records",records);
        R r = R.OK();
        r.setData(map);
        return r;
    }

    //查询车辆
    @GetMapping("all")
    public R selectAll(){
        List<TCar> cars = carService.list();
        R r =R.OK();
        r.setData(cars);
        return r;
    }

    //根据承运商查找对应车辆
    @GetMapping("/aa/{carrierId}")
    public R getByCarrierId(@PathVariable  Integer carrierId){
        System.out.println("CarController.getByCarrierId");
//        System.out.println(carrierId);
        R r= null;
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("carrier_id",carrierId);
        List list = carService.list(queryWrapper);
        System.out.println("list = " + list);

        if(list != null){
           r=R.OK();
           r.setData(list);
          }else {
              r=R.ERROR();
          }
//        System.out.println(r);
          return r;
    }

    //根据承运商查找对应车辆
    @GetMapping("/bb/{carrierId}")
    public R getByCarrierId1(@PathVariable  Integer carrierId){
        System.out.println("CarController.getByCarrierId");
//        System.out.println(carrierId);
        R r= null;
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("carrier_id",carrierId);
        List list = carService.list(queryWrapper);
        System.out.println("list = " + list);

        if(list != null){
            r=R.OK();
            r.setData(list);
        }else {
            r=R.ERROR();
        }
//        System.out.println(r);
        return r;
    }
}
